package com.lizard.schedule.utils;

import android.util.Log;

import com.lizard.schedule.R;
import com.lizard.schedule.manager.AccountManager;
import com.lizard.schedule.net.http.request.bean.ScheduleSync.ScheduleSyncItem;
import com.lizard.schedule.persistence.db.bean.Schedule;
import com.lizard.schedule.persistence.db.table.ScheduleTbl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 计划“重复”相关帮助类
 */
public class RepeatScheduleUtil {

    /**
     * 检测repeat1是否包含repeat2
     *
     * @param repeatType1
     * @param repeatType2
     * @return
     */
    public static boolean containRepeat(int repeatType1, int repeatType2){
        if((repeatType1 | repeatType2) == repeatType1){
            return true;
        } else{
            return false;
        }
    }

    /**
     * repeat值合并
     *
     * @param repeat1
     * @param repeat2
     * @return
     */
    public static int mergeRepeat(int repeat1, int repeat2){
        return repeat1 | repeat2;
    }

    /**
     * 将在数据库中的"重复"计划的时间更新到下一个最近发生的计划时间
     */
    public static void updateRepeatScheduleDb(){
        List<Schedule> schedules = ScheduleTbl.getInstance().selectRepeatSchedules(AccountManager.getInstance().getmUsernameMD5L16());
        if(schedules != null){
            List<Schedule> needUpdateSchedules = new ArrayList<Schedule>();
            long nowTime = System.currentTimeMillis();
            for(Schedule schedule: schedules){
                long newTime = generateNextRepatBeginTime(schedule.getBeginTime(), nowTime, schedule.getRepeat());
                if(newTime != schedule.getBeginTime()){
                    schedule.setStatus(Schedule.STATUS_UNFINISHED); // 设置为已完成
                    schedule.setBeginTime(newTime);
                    needUpdateSchedules.add(schedule);
                }
            }
            if(needUpdateSchedules.size() > 0){
                ScheduleTbl.getInstance().batchUpdate(AccountManager.getInstance().getmUsernameMD5L16(), needUpdateSchedules);
            }
        }
    }

    /**
     * 生成“重复”计划最近的时间
     *
     * @param scheduleBeginTime
     * @param nowTime
     * @param repeat
     * @return
     */
    public static long generateNextRepatBeginTime(long scheduleBeginTime, long nowTime, int repeat){
        long newTime = 0;

        int[] monday2SundayRepeatValue = new int[]{
                ScheduleSyncItem.REPEAT_MONDAY,
                ScheduleSyncItem.REPEAT_TUESDAY,
                ScheduleSyncItem.REPEAT_WEDNESDAY,
                ScheduleSyncItem.REPEAT_THURSDAY,
                ScheduleSyncItem.REPEAT_FRIDAY,
                ScheduleSyncItem.REPEAT_SATURDAY,
                ScheduleSyncItem.REPEAT_SUNDAY};

        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(nowTime);

        // 当前时间属于周几
        int nowDayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);// 周日：1 周一：2 周二：3 ... 周六：7
        nowDayOfWeek = nowDayOfWeek - 1;
        nowDayOfWeek = (nowDayOfWeek == 0 ? nowDayOfWeek = 7: nowDayOfWeek);

        int nowYear = calendar.get(Calendar.YEAR);
        int nowMonth = calendar.get(Calendar.MONTH);
        int nowDayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);

        calendar.setTimeInMillis(scheduleBeginTime);
        calendar.set(Calendar.YEAR, nowYear);
        calendar.set(Calendar.MONTH, nowMonth);
        calendar.set(Calendar.DAY_OF_MONTH, nowDayOfMonth);

        // 日期为今天的计划开始时间
        long todayScheduleBeginTime = calendar.getTimeInMillis();

        // 先和本周的所有可能的时间对比
        for(int i = 1; i <= monday2SundayRepeatValue.length; i++){
            if(newTime == 0
                    && nowDayOfWeek <= i
                    && RepeatScheduleUtil.containRepeat(repeat, monday2SundayRepeatValue[i - 1])){
                long tmpTime = todayScheduleBeginTime + (i - nowDayOfWeek) * TimeUtil.DAY_MILLIS;
                if(tmpTime > nowTime){
                    newTime = tmpTime;
                }
            }
        }

        if(newTime == 0){

            // 下周一的计划开始时间
            long nextWeekScheduleBeginTime = todayScheduleBeginTime + (7 - nowDayOfWeek + 1) * TimeUtil.DAY_MILLIS;

            // 在和下周的所有可能的时间对比
            for(int i = 1; i <= monday2SundayRepeatValue.length; i++){
                if(newTime == 0
                        && RepeatScheduleUtil.containRepeat(repeat, monday2SundayRepeatValue[i - 1])){
                    long tmpTime = nextWeekScheduleBeginTime + (i - 1) * TimeUtil.DAY_MILLIS;
                    if(tmpTime > nowTime){
                        newTime = tmpTime;
                    }
                }
            }

        }

        if(newTime == 0){
            newTime = scheduleBeginTime;
        }

        return newTime;
    }

}
